document.getElementById('year') ? layui.use(['laydate'], function (laydate) {
	laydate.render({
		elem: '#year',
		type: 'month',
		theme: '#165DFF',
		value: fullYear + "-" + fullMonth,
		//,range: true 
		//,trigger: 'click'
		done: function (value, date, endDate) {
			// console.log(value, date, endDate);
			fullYear = value.split('-')[0]
			fullMonth = value.split('-')[1]

			localStorage.setItem('year', fullYear);
			localStorage.setItem('month', fullMonth);

			change()
		},
		change: function (value, date, endDate) {
			//this.elem.val(value)
		}
	});
}) : ''


function yewuTreeData() {
	const username = localStorage.getItem("user");
	const currYear = localStorage.getItem("year");
	const currMon = localStorage.getItem("month");
	const currDept = localStorage.getItem("dept");

	console.log(username, 'username')
	//来款执行率树状图
	var obj = {
		username: username,
		budgetYear: currYear,
		budgetMonth: currMon,
		// dept: currDept == 'null' ? '' : currDept,
		dept: 2022,
		status: 2,
		chartPage: 70403,
		chartNo: 1
	}
	ApiGet('getdata', obj, function (e) {
		console.log('请求ing')
		var json = JSON.parse(e);
		var chartData = json.items[0].charts[0].data;
		if (chartData.length > 0) {
			// console.log(chartData.length)
			// document.getElementById('nodata').style.display = 'none'
			$('#nodata').html(``);

			console.log(chartData, "来款情况树形结构数据")
			let treeData = []
			chartData.forEach((item) => {
				let obj = {
					key: item.key01,
					parent: item.key02,
					name: item.key01,
					implementationRate: (item.key05 * 100).toFixed(1),
					dw: '万元',
					ys: '来款预算',
					ysmoney: (item.key03 / 10000).toFixed(2),
					zx: '实际来款',
					zxmoney: (item.key04 / 10000).toFixed(2),
				}
				treeData.push(obj);
			})
			let firstArr = {
				key: treeData[0].key,
				parent: treeData[0].parent,
				ALL_name: treeData[0].name,
			}
			//修改数组中的第一个对象 
			treeData = treeData.map(item => {
				return item.key === firstArr.key ? firstArr : item;
			})
			// console.log(treeData[0], '修改数组中的第一个对象 ')


			var myDiagramDiv1 = document.getElementById('myDiagramDiv1');
			var wrapper = document.getElementById('wrapper');
			wrapper.removeChild(myDiagramDiv1)
			var div = document.createElement('div');
			div.setAttribute('id', 'myDiagramDiv1')
			wrapper.appendChild(div);

			var G = go.GraphObject.make;
			var myDiagram =
				G(go.Diagram, "myDiagramDiv1", {
					initialAutoScale: go.Diagram.Uniform,
					"toolManager.mouseWheelBehavior": go.ToolManager.WheelZoom,
					initialDocumentSpot: go.Spot.Top,
					initialViewportSpot: go.Spot.Top,

					initialContentAlignment: go.Spot.Top, // 居中显示Diagram内容
					// "undoManager.isEnabled": true, // 打开ctrl-z撤销和ctrl-y重做功能
					layout: G(go.TreeLayout, //1个特殊的树形排列Diagram.layout布局
						{
							angle: 90,
							layerSpacing: 50,
						})
				});
			// 设置缩放 按钮
			document.getElementById('zoomToFit').addEventListener('click', () => myDiagram.commandHandler.zoomToFit());

			document.getElementById('centerRoot').addEventListener('click', () => {
				myDiagram.scale = 1;
				// myDiagram.commandHandler.scrollToPart(myDiagram.findNodeForKey(1));
			});

			function textStyle(field) {
				if (field == "ALL_name") {
					return [{
							font: "bold 16pt sans-serif",
							stroke: "#000",
							visible: false //仅在有数据时候显示
						},
						new go.Binding("visible", field, val => val !== undefined)
					]
				}
				return [{
						font: "bold 14pt sans-serif",
						stroke: "#000",
						visible: false //仅在有数据时候显示
					},
					new go.Binding("visible", field, val => val !== undefined)
				]
			}

			function fontextStyle(field) {
				if (field == "ys" || "zx" || "sy") {
					return [{
							font: "12pt  Segoe UI,sans-serif",
							stroke: "#4E5969",
							visible: false //仅在有数据时候显示
						},
						new go.Binding("visible", field, val => val !== undefined)
					]
				}
				return [{
						font: "12pt  Segoe UI,sans-serif",
						stroke: "#4E5969",
						visible: false //仅在有数据时候显示
					},
					new go.Binding("visible", field, val => val !== undefined)
				]
				// return { font: "12pt  Segoe UI,sans-serif", stroke: "gray" };
			}

			function dwtextStyle(field) {
				if (field == "ALL_dw") {
					return [{
							font: "9pt  Segoe UI,sans-serif",
							stroke: "#4E5969",

							visible: false //仅在有数据时候显示
						},
						new go.Binding("visible", field, val => val !== undefined)
					]
				}
				return [{
						font: "9pt  Segoe UI,sans-serif",
						stroke: "#4E5969",
						visible: false //仅在有数据时候显示
					},
					new go.Binding("visible", field, val => val !== undefined)
				]
				// return { font: "9pt  Segoe UI,sans-serif", stroke: "gray" };
			}

			function moneytextStyle(field) {
				if (field == "ALL_zxmoney" || "ALL_ysmoney" || "ALL_symoney") {
					return [{
							font: "bold 14pt sans-serif",
							stroke: "#1D2129",
							visible: false //仅在有数据时候显示
						},
						new go.Binding("visible", field, val => val !== undefined)
					]
				}
				return [{
						font: "bold 14pt sans-serif",
						stroke: "#1D2129",
						visible: false //仅在有数据时候显示
					},
					new go.Binding("visible", field, val => val !== undefined)
				]
				// return { font: "bold 14pt sans-serif", stroke: "#000" };
			}

			myDiagram.nodeTemplate =
				G(go.Node, "Auto", {
						isTreeExpanded: false,
						selectionObjectName: "BODY",
						deletable: true,
						locationSpot: go.Spot.Top,
						isShadowed: true,
						shadowBlur: 10,
					},
					// 定义节点的外部形状
					G(go.Shape, "RoundedRectangle", {
						name: "SHAPE",
						fill: "#fff",
						strokeWidth: 0,
						portId: "",
						cursor: "pointer",
						// click: tanchuMask
					}),
					//总名称
					G(go.Panel, "Vertical", {
							width: 150,
							height: 60,
							defaultStretch: go.GraphObject.Horizontal,
						},
						G(go.TextBlock, textStyle("ALL_name"), // 名称---all
							{
								// row: 2,
								// column: 2,
								font: "bold 16pt sans-serif",
								// editable: true,
								// isMultiline: false,
								// minSize: new go.Size(10, 16),
								margin: new go.Margin(10, 10, 0, 10),
								text: "textAlign: 'center'",
								// margin: 10,
								textAlign: "center",
								// background: "#165DFF"
							},
							new go.Binding("text", "ALL_name").makeTwoWay(), ),
					),
					G(go.Panel, "Vertical", {
							defaultStretch: go.GraphObject.Horizontal,
						},
						//个部分名称
						G(go.Panel, "Table",
							G(go.TextBlock, textStyle("name"), // 名称
								{
									// row: 1, column: 1,
									font: "bold 14pt sans-serif",
									editable: true,
									isMultiline: false,
									minSize: new go.Size(10, 16),
									margin: 8,
									alignment: go.Spot.Center
								},
								new go.Binding("text", "name").makeTwoWay()),
						),
						// echarts部分
						G(go.Panel, "Table", {
								stretch: go.GraphObject.Horizontal,
								width: 200,
								height: 80,
								margin: 15,
								visible: false,
								background: "#E8F3FF",
							},
							new go.Binding("visible", "implementationRate", imp => imp !== undefined),
							G(go.TextBlock, // 执行率
								{
									row: 0,
									column: 0,
									font: "bold 16pt sans-serif",
									alignment: go.Spot.Right,
									stroke: "#165DFF",
								},
								new go.Binding("text", "implementationRate").makeTwoWay()),
							G(go.TextBlock, "%", // 执行率
								{
									row: 0,
									column: 1,
									font: "bold 9pt sans-serif",
									margin: 10,
									alignment: go.Spot.Left,
									stroke: "#165DFF",
								},
							),
							G(go.TextBlock, "执行率", // 执行率
								{
									row: 1,
									column: 0,
									font: "bold 9pt sans-serif",
									stroke: "#4E5969",
									// minSize: new go.Size(30, 16),
									margin: new go.Margin(-5, 0, 10, 0),
									alignment: go.Spot.Right,

								},
							),

						),
						// notes部分
						G(go.Panel, "Table", {
								margin: new go.Margin(6, 10, 0, 6),
								stretch: go.GraphObject.Horizontal,
								defaultAlignment: go.Spot.Left, //对齐方式
							},
							// 111111111111111111111111
							G(go.TextBlock, fontextStyle("ys"), // 预算金额:
								{
									row: 4,
									column: 0,
									margin: 5
								},
								new go.Binding("text", "ys").makeTwoWay()
							),
							G(go.TextBlock, moneytextStyle("ysmoney"), //  预算金额:(data)
								{
									row: 4,
									column: 8,
									editable: true,
									isMultiline: false,
									minSize: new go.Size(10, 14),
									margin: new go.Margin(0, 0, 0, 5),
									alignment: go.Spot.Right,
								},
								new go.Binding("text", "ysmoney").makeTwoWay()),
							G(go.TextBlock, dwtextStyle("dw"), //单位
								{
									row: 4,
									column: 10,
									margin: new go.Margin(0, 0, 0, 5)
								},
								new go.Binding("text", "dw").makeTwoWay()),
							// 111111111111111111111111
							G(go.TextBlock, fontextStyle("zx"), //执行金额
								{
									row: 6,
									column: 0,
									margin: 5
								},
								new go.Binding("text", "zx").makeTwoWay()
							),
							G(go.TextBlock, moneytextStyle("zxmoney"), {
									row: 6,
									column: 8,
									editable: true,
									isMultiline: false,
									minSize: new go.Size(10, 14),
									alignment: go.Spot.Right,
								},
								new go.Binding("text", "zxmoney").makeTwoWay()),
							G(go.TextBlock, dwtextStyle("dw"), {
									row: 6,
									column: 10,
									margin: new go.Margin(0, 0, 0, 5)
								},
								new go.Binding("text", "dw").makeTwoWay()),
						),
						//折叠展开按钮+
						G(go.Panel, "Table", {
								margin: new go.Margin(20, 0, 0, 0),
								height: 17,
								alignment: go.Spot.Center,
							},
							G("TreeExpanderButton", {
								"ButtonBorder.fill": "#4E5969",
								"ButtonBorder.figure": "Circle",
								"_buttonFillOver": "#165DFF",
								"_buttonStrokeOver": "#165DFF",
								"_buttonFillPressed": "#FFF",
							}, )
						),
					));

			myDiagram.linkTemplate =
				G(go.Link, {
						routing: go.Link.Orthogonal,
						deletable: false,
						corner: 10
					},
					G(go.Shape, {
						strokeWidth: 1,
						stroke: "#CED1D9"
					}),
				);
			var model = G(go.TreeModel);
			myDiagram.addDiagramListener("InitialLayoutCompleted", function (e) {
				e.diagram.findTreeRoots().each(function (r) {
					r.expandTree(2);
				});
			});

			model.nodeDataArray = treeData
			myDiagram.model = model;
		} else {
			console.log('暂无数据！')
		}
	});

	//来款执行率说明情况
	var obj2 = {
		username: username,
		budgetYear: currYear,
		budgetMonth: currMon,
		// dept: currDept == 'null' ? '' : currDept,
		dept: 2022,
		status: 2,
		chartPage: 70403,
		chartNo: 2
	}
	ApiGet('getdata', obj2, function (res) {
		let re = eval("(" + res + ")");
		if (re.items[0].charts[0].data.length > 0) {
			$('.description  p').html(`${re.items[0].charts[0].data[0].key01}`)
		}
	});


}

go.GraphObject.defineBuilder('TreeExpanderButton', function (args) {
	var button = /** @type {Panel} */ (
		go.GraphObject.make('Button', { // 为isTreeExpanded绑定转换设置这些值
				'_treeExpandedFigure': 'MinusLine',
				'_treeCollapsedFigure': 'PlusLine'
			},
			go.GraphObject.make(go.Shape, // 图标
				{
					name: 'ButtonIcon',
					figure: 'MinusLine', // isTreeExpanded的默认值为true
					stroke: '#fff',
					strokeWidth: 2,
					desiredSize: new go.Size(8, 8)
				},
				//绑定形状。将图形添加到节点。isTreeExpanded值使用此转换器：
				new go.Binding('figure', 'isTreeExpanded',
					function (exp, shape) {
						var but = shape.panel;
						return exp ? but['_treeExpandedFigure'] : but['_treeCollapsedFigure'];
					}
				).ofObject()
			),
			// 假设最初不可见，因为没有链接出来
			{
				visible: false
			},
			//将按钮可见性绑定到它是否为叶节点
			new go.Binding('visible', 'isTreeLeaf',
				function (leaf) {
					return !leaf;
				}
			).ofObject()
		)
	);

	// 树展开/折叠
	button.click = function (e, btn) {
		console.log(9999)
		var node = btn.part;
		if (node instanceof go.Adornment) node = node.adornedPart;
		if (!(node instanceof go.Node)) return;
		var diagram = node.diagram;
		if (diagram === null) return;
		var cmd = diagram.commandHandler;
		if (node.isTreeExpanded) {
			if (!cmd.canCollapseTree(node)) return;
		} else {
			if (!cmd.canExpandTree(node)) return;
		}
		e.handled = true;
		if (node.isTreeExpanded) {
			cmd.collapseTree(node);
		} else {
			cmd.expandTree(node);
		}
	};
	return button;
});


$('.mask-wrap .btn button.save').click(function () {
	ApiGet('getdata', {
		username: localStorage.getItem("user"),
		budgetYear: localStorage.getItem("year"),
		status: 2,
		chartPage: 70403,
		chartNo: 3,
		key01: $('.mask-wrap .mask-write-area textarea').val()
	}, function (res) {
		let re = eval("(" + res + ")");
		$('#editMask').hide()
		alert(re.items[0].charts[0].chart_name)
	});

	ApiGet('getdata', {
		username: localStorage.getItem("user"),
		budgetYear: localStorage.getItem("year"),
		budgetMonth: localStorage.getItem("month"),
		// dept: currDept == 'null' ? '' : currDept,
		dept: 2022,
		status: 2,
		chartPage: 70403,
		chartNo: 2
	}, function (res) {
		let re = eval("(" + res + ")");
		if (re.items[0].charts[0].data.length > 0) {
			$('.description  p').html(`${re.items[0].charts[0].data[0].key01}`)
		}
	});
})